﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="thread.aspx.cs" Inherits="forums_thread" ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ import Namespace="RobozzleModel" %>
<%@ import Namespace="System.Linq" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RoboZZle Forums</title>
    <link href="../stats.css" rel="stylesheet" type="text/css"/>
    <script language="javascript" type="text/javascript">
    <!--
        function checkComment() {
            var len = document.getElementsByName('text')[0].value.length;
            if (len > 4000) {
                alert('The maximum comment length is 4000 characters.\n\nYou\'ve entered ' + len + '.');
                return false;
            }
            return true;
        }
    -->
    </script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
<%
    RobozzleEntities entities = new RobozzleEntities();
    entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entities.ForumCategory);
    entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entities.ForumThread);
    entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entities.ForumComment);
    
    string threadIdStr = Request.Params["id"];
    string puzzleStr = Request.Params["puzzle"];
    int threadId;
    ForumThread thread = null;

    if (int.TryParse(threadIdStr, out threadId))
    {
        thread = entities.ForumThread.Where(c => c.Id == threadId).FirstOrDefault();
    }
    
    if (thread == null)
    {
        // Try to find the thread for this puzzle
        int puzzleId;
        if (int.TryParse(puzzleStr, out puzzleId))
        {
            thread = entities.ForumThread.Where(row => row.PuzzleId == puzzleId).FirstOrDefault();
        }
        
    }
    
    if (thread == null)
    {
        Response.Write("Invalid thread id.");
        return;
    }    
        
        
    //
    // Post a new comment?
    //
    string userArg = Request.Params["user"];
    string passwordArg = Request.Params["password"];
    string textArg = Request.Params["text"];
    bool invalidPwd = false;
    bool postSuccess = false;

    if (textArg != null)
    {
        passwordArg = StatsCommon.HashPassword(passwordArg);
        if (entities.User.Any(user => user.Username == userArg && user.Password == passwordArg))
        {
            entities.AddToForumComment(
                new ForumComment
                {
                    Comment = textArg,
                    Date = DateTime.Now,
                    ForumThread = thread,
                    UserName = userArg,
                    IpAddress = Request.UserHostAddress
                });
            entities.SaveChanges();
            postSuccess = true;
            textArg = "";
        }
        else
        {
            invalidPwd = true;
        }
    }
%>

<%= StatsCommon.MenuBar(MenuPage.Forums) %>
<h1>RoboZZle Forums</h1>
<p><%= StatsCommon.MakeForumPath(thread.ForumCategory.Id, Server.HtmlEncode(thread.ForumCategory.Title), Server.HtmlEncode(thread.ThreadTitle))%></p>    

<% if (thread.PuzzleId != null)
   { %>
    <p>Thread for puzzle <a href="/puzzle.aspx?id=<%= thread.PuzzleId%>"><%= thread.ThreadTitle %></a>. Hints are OK, but please no full solutions.</p>
<% } %>

<%
    if (invalidPwd)
    {
        Response.Write("<p style=\"color:#e88\">Invalid username / password.</p>");
    }
    else if (postSuccess)
    {
        Response.Write("<p style=\"color:#aea\">Comment posted.</p>");
    }
    
    foreach (var comment in thread.ForumComment.OrderBy(comment => comment.Id))
    {
        // Yeah... we are going to get *this* hacky...
        bool fromGame = comment.IpAddress == null || comment.IpAddress.Trim() == "";
        %>
        
        <div class="forumComment">
            <div style="margin:10px; font-size:small;"><%= Server.HtmlEncode(comment.Comment).Replace("\n", "<br>") %></div>
            <p class="forumCommentFooter"><%= StatsCommon.UserLink(comment.UserName) + (fromGame ? "'s comment after solving the puzzle" : "")%>, <%= StatsCommon.FormatDate(comment.Date) %>.</p>
        </div>
        
        <%
    }
%>

    <p>&nbsp;</p>
    <p>Reply to thread &quot;<%= thread.ThreadTitle %>&quot;</p>
    <form name="postcomment" action="thread.aspx" method="post">
        <div class="forumComment">
            <table>
                <tr>
                    <td style="width:60px;text-align: left;">User:</td>
                    <td style="text-align:left"><input name="user" type="text" style="width:100px" value="<%= Server.HtmlEncode(userArg) %>"/></td>
                </tr>
                <tr>
                    <td style="width:60px;text-align: left;">Password:</td>
                    <td><input name="password" type="password" style="width:100px"/></td>
                </tr>
                <tr>
                    <td colspan="2"><textarea id="text" name="text" style="width:100%" rows="6"><%= Server.HtmlEncode(textArg) %></textarea></td>
                </tr>     
                <tr>
                    <td colspan="2"><asp:Button OnClientClick="return checkComment()" runat="server" ID="btnSend" Text="Post comment" /></td>
                </tr>
            </table> 
             
        </div>
    </form>
    </div>
    </form>
<%
     Response.Write(StatsCommon.Footer());
%>
</body>
</html>
